<?php

namespace addons\resources\model;

/**
 * 资源信息
 */
class Resources extends \web\common\model\BaseModel {

    protected function _initialize() {
        $this->tableName = 'resources';
    }

    /**
     * 获取列表数据
     * @param type $offset 开始位置
     * @param type $length 每页数量
     * @param type $filter 过滤条件
     * @param type $fields 字段信息
     * @param type $order 排序     
     * @return type
     */
    public function getList($offset, $length, $filter = '', $fields = '', $order = 'id desc') {
        $where = '';
        if (!empty($filter))
            $where = $filter;
        $m = $this;
        if (!empty($fields))
            $m = $m->field($fields);
        if (!empty($order))
            $m = $m->order($order);
        if ($length < 0)
            return $m->where($where)->select();
        else
            return $m->where($where)->limit($offset, $length)->select();
    }

    public function getPicListByIds($img_ids) {
        if (!empty($img_ids)) {
            $sql = 'select id,file_path as img_url,file_name from ' . $this->getTableName() . ' a where id in (' . $img_ids . ')';
            return $this->query($sql);
        } else {
            return null;
        }
    }
    
    public function getVideoList($pageIndex = -1, $pageSize = -1, $filter = '', $fields = '', $order = 'id desc'){
        $videoClassM = new \addons\resources\model\VideoClass();
        $sql = 'select ';
        if (!empty($fields))
            $sql .= $fields;
        else
            $sql .= '*';
        $sql .= ' from ' . $this->getTableName();
        if (!empty($filter))
            $sql .= ' where ' . $filter;
        $sql = 'select * from ('.$sql.') as a ';
        return $this->getDataListBySQL($sql, $pageIndex, $pageSize, $order);
    }
    
//    select a.*,if(b.id,1,0) as is_like from tp_resources a left join `tp_like_video_record` b on b.user_id=3296 and FIND_IN_SET(a.id,video_ids)
    public function getVideoAndLikeList($user_id, $pageIndex = -1, $pageSize = -1, $filter = '', $fields = '', $order = 'id desc'){
        $videoClassM = new \addons\resources\model\VideoClass();
        $videoLikeM = new \addons\resources\model\LikeVideoRecord();
        $sql = 'select ';
        if (!empty($fields))
            $sql .= $fields;
        else
            $sql .= '*';
        $sql .= ' from ' . $this->getTableName();
        if (!empty($filter))
            $sql .= ' where ' . $filter;
        $sql = 'select a.*,b.video_class_name,if(c.id,1,0) as is_like from ('.$sql.') as a left join '.$videoClassM->getTableName().' b on a.class_id = b.id'
                .' left join '.$videoLikeM->getTableName().' c on c.user_id='.$user_id.' and FIND_IN_SET(a.id,video_ids)';
                
        return $this->getDataListBySQL($sql, $pageIndex, $pageSize, $order);
    }
}
